from WebHandler.IWebHandler import IWebHandler
class WebHandler(IWebHandler):


    def __init__(self,currentElement):
        self.current = currentElement

    def setValueById(self, id,value):
        self.current.find_element_by_id(id).send_keys(value)


    def setValueByName(self, name,value):
        self.current.find_element_by_name(name).send_keys(value)

    def getValueById(self, id):
        return self.current.find_element_by_id(id)

    def getValueByName(self, name):
        return self.current.find_element_by_name(name).get_property('value')

    def getValueByClass(self, className):
        return self.current.find_element_by_class_name(className).get_property('value')

    def clickById(self, id):
        self.current.find_element_by_id(id).click()

    def getOuterHtml(self):
        return self.current.find_element_by_xpath("//*").get_attribute("outerHTML")


    def getValuesByClass(self, className):
        value = []
        names = self.current.find_elements_by_class_name(className)
        for val in names:
            value.append(val.get_attribute('value'))
        return value

    def getValuesByName(self, name):
        value = []
        names = self.current.find_elements_by_name(name)
        for val in names:
            value.append(val.get_attribute('value'))
        return value


    def getTextsByClass(self, className):
        value = []
        names = self.current.find_elements_by_class_name(className)
        for val in names:
            value.append(val.text)
        return value

    def getTextsByName(self, name):
        value = []
        names = self.current.find_elements_by_name(name)
        for val in names:
            value.append(val.text)
        return value

    def moveElementById(self, id):
        self.current = self.current.find_element_by_id(id)
        return self

    def moveElementByClass(self, className):
        self.current = self.current.find_elements_by_class_name(className)
        return self

    def moveElementByTag(self, tag):
        self.current = self.current.find_elements_by_tag_name(tag)
        return self

    def getCurrentElement(self):
        return self.current

    def getTextByNameAt(self, name, index):
        return self.current[index].find_element_by_name(name).text

    def getTextByClassAt(self, className, index):
        return self.current[index].find_element_by_class_name(className).text

    def getAttrByXPathAt(self, attrName, xpath, index):
        return self.current[index].find_element_by_xpath(xpath).get_attribute(attrName)

    def getSize(self):
        return len(self.current)

    def moveElementByXPath(self, xpath):
        self.current = self.current.find_element_by_xpath(xpath)
        return self

    def getTextByXPath(self, xpath):
        return self.current.find_element_by_xpath(xpath).text


    def getTextByXPathAt(self, xpath, index):
        return self.current[index].find_element_by_xpath(xpath).text


    def getTextByTagAt(self, tagName, index):
        return self.current[index].find_element_by_tag_name(tagName).text


    def setCurrentElement(self, currentElement):
        self.current = currentElement
        return self

    def getHrefByTagAt(self, tagName, index):
        return self.current[index].find_element_by_tag_name(tagName).get_attribute('href')
